package toman;

/**
 * This interface is implemented by query engine selectors. A query engine selector is responsible for selecting
 * the proper query engine for the given task required by TOMan. These tasks are:
 * <ul>
 *  <li>Loading a TO raw data for a given key
 *  <li>Creating a TO query
 *  <li>Updating the underlying storage with the given TO.
 * </ul>
 * <p>
 * User: zslajchrt
 * Date: 2.5.11
 * Time: 14:00
 */
public interface QueryEngineSelector {

    /**
     * Selects the proper query engine for loading a TO.
     * @param toman TOMan
     * @param toClass the TO class
     * @param toId
     * @return
     * @throws NoQueryEngineFoundException thrown if no proper query engine found
     */
    QueryEngine selectEngineForFindInitObject(TOMan toman, Class<?> toClass, Object toId);

    /**
     * Selects the proper query engine for updating.
     * @param toman TOMan
     * @param to the TO
     * @return
     * @throws NoQueryEngineFoundException thrown if no proper query engine found
     */
    QueryEngine selectEngineForCreateQueryForUpdate(TOMan toman, Object to);

    /**
     * Selects the proper query engine for creating the query for the given query definition.
     * @param toman TOMan
     * @param toClass tre TO class
     * @param queryDefinition the query definition
     * @return the query engine
     * @throws NoQueryEngineFoundException thrown if no proper query engine found
     */
    QueryEngine selectEngineForCreateQuery(TOMan toman, Class<?> toClass, Object queryDefinition);

    /**
     * Thrown if no proper query engine found
     */
    class NoQueryEngineFoundException extends RuntimeException {

        private static final long serialVersionUID = 135266469065827843L;

        transient private final Object contextInfo;

        public NoQueryEngineFoundException(Object contextInfo) {
            super("" + contextInfo);
            this.contextInfo = contextInfo;
        }

        public Object getContextInfo() {
            return contextInfo;
        }
    }

}
